// comp/rich-text/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        editorCtx: {},
        richContent: ''
    },
    // 编辑器初始化完成时触发
    onEditorReady() {
        wx.createSelectorQuery().select('#editor').context(res => {
            this.editorCtx = res.context;
        }).exec();
    },
    // 初始化设置一段富文本数据
    setContents() {
        this.editorCtx.setContents({
            html: `<div><h2>初始化设置富文本的内容</h2><p>一般用来将咱们保存的草稿写进富文本编辑框内容中</p></div>`,
            success: function() {
                console.log('插入富文本内容成功')
            }
        })
    },
    choseAndInsertImage() {
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: res => {
                this.editorCtx.insertImage({
                    src: res.tempFilePaths[0],
                    success: function() {
                        console.log('插入图片成功');
                    }
                })
            }
        })
    },
    onInput(event) {
        console.log(event);
        this.setData({
            richContent: event.detail.html
        })
    },
    undo() {
        this.editorCtx.undo()
    },
    redo() {
        this.editorCtx.redo()
    },
    clear() {
        this.editorCtx.clear()
    },
    getContents() {
        this.editorCtx.getContents({
            success: res => {
                this.setData({
                    richContent: res.html
                })
            }
        })
    }

})